package com.qdu.servlet;

import com.qdu.entity.Comment;
import com.qdu.entity.Reply;
import com.qdu.entity.User;
import com.qdu.service.ReplyService;
import com.qdu.service.impl.ReplyServiceImpl;
import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Ice
 */
@WebServlet("/ars_mxz")
public class AddReplyServlet extends HttpServlet {

    ReplyService replyService = new ReplyServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            // 得到该登录用户的id号
            User user = new User();
            HttpSession session = req.getSession();
            user = (User) session.getAttribute("user");
            int uid = user.getId();

            // 被回复用户编号
            int rid = Integer.parseInt(req.getParameter("usrid"));

            //回复内容 需要表单内输入
            String contents = req.getParameter("contents");

            // 回复哪个评论
            int cid = Integer.parseInt(req.getParameter("cid"));

            // 得到当前时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            java.util.Date date = new java.util.Date();
            String dateStr = sdf.format(date);
            java.util.Date mydate = sdf.parse(dateStr);

            Reply reply = new Reply(uid, rid, contents, cid, mydate);

            int rows = replyService.addNewReply(reply);

            //生成响应内容，这里直接返回了受影响的行数，用于判断是否添加成功      
            resp.getWriter().println(rows);

        } catch (ParseException ex) {
            Logger.getLogger(AddReplyServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}
